package com.bum.guru.keeng.service.helper;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.bum.guru.exp.keeng.om.KeengAlbum;
import com.bum.guru.keeng.bo.KeengParser;
import com.bum.guru.keeng.dao.KeengAlbumDAO;
import com.bum.guru.util.BaseParser;

public class LoadKeengAlbumHelper implements ApplicationContextAware {
	Logger			log		= Logger.getLogger(getClass());

	KeengParser		keengParser;
	KeengAlbumDAO	keengAlbumDAO;

	private int		page	= 130;

	@Override
	public void setApplicationContext(ApplicationContext arg0) throws BeansException {
		Executors.newScheduledThreadPool(1).scheduleWithFixedDelay(new Runnable() {
			@Override
			public void run() {
				try {
					loadAlbums(page);
					if (page > 0) {
						page--;
					}
				} catch (Throwable e) {
					e.printStackTrace();
				}
			}
		}, 15, 7200, TimeUnit.SECONDS);
	}

	public void loadAlbums(int page) throws Exception {
		log.debug("Loading page " + page);
		List<String> codes = fetchAlbumPage(page);
		for (int i = codes.size() - 1; i >= 0; i--) {
			String code = codes.get(i);
			log.debug(page + " Load album " + code);
			if (keengAlbumDAO.get(code) != null) {
				log.debug("Nạp rồi...");
				continue;
			}
			KeengAlbum album = null;
			try {
				album = keengParser.getAlbum(code);
			} catch (Exception e) {
				log.debug("Lỗi album " + code, e);
			}
			if (album != null) {
				log.debug("Save album: " + album.getTitle());
				keengAlbumDAO.add(album);
			}
		}
	}

	private List<String> fetchAlbumPage(int page) {
		String url = "http://www.keeng.vn/album/album-moi.html?page=" + page;
		String html = BaseParser.loadHtml(url);
		html = BaseParser.parseContent(html, "<div class=\"album-item\">", "<div class=\"keeng_pagging");

		String[] parts = html.split("<div class=\"album-item\">");
		List<String> codes = new ArrayList<String>(parts.length);
		for (String part : parts) {
			part = BaseParser.parseContent(part, "href=\"", ".html\">");
			part = part.substring(part.lastIndexOf("/") + 1);
			codes.add(part);
		}
		return codes;
	}

//	public boolean load(long id) {
//		String url = "http://matxac.com/Play/1/" + id + ".xml";
//		System.out.println(id);
//		String html = BaseParser.loadHtml(url);
//		String name = BaseParser.parseContent(html, "<Title>", "</Title>");
//		String track = BaseParser.parseContent(html, "<location>", "</location>");
//
//		if (StringUtils.isNotBlank(name) && StringUtils.isNotBlank(track)) {
//			try {
//				System.out.println(id + " " + name + " " + track);
//				songDAO.add(id, name, track);
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
//			return true;
//		}
//		return false;
//	}

	public void setKeengAlbumDAO(KeengAlbumDAO keengAlbumDAO) {
		this.keengAlbumDAO = keengAlbumDAO;
	}

	public void setKeengParser(KeengParser keengParser) {
		this.keengParser = keengParser;
	}

	public void setPage(int page) {
		this.page = page;
	}

}
